const promise = require('bluebird')
const config = require('config');
const axios = require('axios')
const { JSDOM } = require('jsdom')
const { map, extend } = require('lodash')
const Iconv = require('iconv').Iconv
const iconv = new Iconv('gb2312', 'utf-8')
const mysql = require('mysql2/promise')
axios.interceptors.response.use(function (response) {
  const ctype = response.headers["content-type"]
  response.data = ctype.includes("charset=GB2312") ?
    iconv.convert(response.data, 'gb2312') :
    response.data
  return response
})
const urls = [
  {
    'category': '电影',
    'url': 'http://top.baidu.com/buzz?b=26&c=1&fr=topcategory_c1'
  },
  {
    'category': '电视剧',
    'url': 'http://top.baidu.com/buzz?b=4&c=2&fr=topcategory_c2'
  },
  {
    'category': '综艺',
    'url': 'http://top.baidu.com/buzz?b=19&c=3&fr=topcategory_c3'
  },
  {
    'category': '动漫',
    'url': 'http://top.baidu.com/buzz?b=23&c=5&fr=topcategory_c5'
  },
]



async function main () {
  const dbconfig = config.get('db')
  const conn = await mysql.createConnection(extend({Promise: promise}, dbconfig))
  const keywords = await promise.all(map(urls, ({category, url}) => {
    return new Promise((reslove, reject) => {
      axios.get(url, {responseType: 'arraybuffer'}).then(({data}) => {
        const {window: {document}} = new JSDOM(data)
        const ks = document.querySelectorAll('.list-table > tbody > tr:not(.item-tr) > .keyword')
        const keyrowds = map(ks, node =>  node.querySelector('.list-title').innerHTML)
        reslove(keyrowds)
      }).then(err => {
        reject(err)
      })
    })
  }))

  const ks = keywords.reduce((p, c) => p.concat(c), [])
  await conn.query('update mac_vod set vod_level = 0 where vod_level = ?', [6])
  await conn.query('update mac_vod set vod_level = 6 where vod_name in(?)', [ks])
  const [rows, fields] = await conn.query('select vod_id,vod_name from mac_vod where vod_level in(?)', [6]);
  console.log(new Date(), 'rows', rows.length)
  // console.log('fields', fields)
  await conn.close();

}

main();
